// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: Sprite.cpp
// Desrciption	: CSprite implementation file.
// Author		: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "GameStd.h"

// Library Includes

// This Include
#include "Sprite.h"

// Local Includes

CSprite::CSprite()
	:m_hBitmap(0),
	m_bIsVisible(false)
{

}

CSprite::CSprite(HBITMAP _hBitmap, bool _bIsVisible)
	:m_hBitmap(_hBitmap), 
	m_bIsVisible(_bIsVisible)
{
	BITMAP bm;

	// Get the bitmap from the handle to retrieve the width and height of the bitmap.
	GetObject(_hBitmap, sizeof(bm), &bm);

	// Add a default mask that masks the entire image.
	AddMask(CRect(0,0, bm.bmWidth, bm.bmHeight));
}

CSprite::CSprite(CSprite& _rSprite)
{
	m_hBitmap = _rSprite.Release();
	m_bIsVisible = _rSprite.IsVisible();
	m_masks = _rSprite.GetAllMasks();
}

CSprite::~CSprite()
{
	if(m_hBitmap) {
		// Delete the bitmap.
		DeleteObject(m_hBitmap);
	}
}

CSprite& CSprite::operator=(CSprite& _rSprite)
{
	m_hBitmap = _rSprite.Release();
	m_bIsVisible = _rSprite.IsVisible();
	m_masks = _rSprite.GetAllMasks();

	return *this;
}

HBITMAP CSprite::GetBitmap() const
{
	return m_hBitmap;
}

HBITMAP CSprite::Release()
{
	HBITMAP hBitmap = m_hBitmap;
	m_hBitmap = NULL;

	return hBitmap;
}

void CSprite::AddMask(const CRect& _krMask)
{
	m_masks.push_back(_krMask);
}

CRect CSprite::GetMask(int _iFrame) const
{
	if(_iFrame >= 0 && static_cast<unsigned int>(_iFrame) < m_masks.size()) {
		// Retrieve the mask at _iFrame.
		return m_masks[_iFrame];
	}
	else {
	// Out of bounds.
		// Return a blank CRect.
		return CRect();
	}
}

const std::vector<CRect>& CSprite::GetAllMasks() const
{
	return m_masks;
}

void CSprite::SetVisible(bool _bIsVisible)
{
	m_bIsVisible = _bIsVisible;
}

bool CSprite::IsVisible() const
{
	return m_bIsVisible;
}


